class Base {
  constructor(msg, data) {
    if (typeof msg !== 'string') {
      this.data = msg
      msg = null
      data = null
    }
    if (msg) {
      this.msg = msg
    }
    if (data) {
      this.data = data
    }
  }
}

class SuccessModel extends Base {
  constructor(msg, data) {
    super(msg, data)
    this.code = 1
  }
}

class FailModel extends Base {
  constructor(msg, data) {
    super(msg, data)
    this.code = -1
  }
}
// 加密函数
const crypto = require('crypto')
const secretKey = 'Wddsade_./123'
// 1. md5 封装一个生成加密的函数
const md5 = (pwd) => {
  return crypto.createHash('md5').update(pwd).digest('hex')
}
// 2. 加强密钥函数
const getPassword = (pwd) => {
  return md5(`pwd=${pwd}_secretKey=${secretKey}`)
}

const svgCaptcha = require('svg-captcha')

function createCode() {
  return svgCaptcha.create()
}

const genID = (length) => {
  return Number(
    Math.random().toString().substr(3, length) + Date.now()
  ).toString(36)
}

module.exports = {
  SuccessModel,
  FailModel,
  getPassword,
  createCode,
  genID,
}
